/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.controller;

import vip.dexian.admin.base.BaseController;
import vip.dexian.admin.command.DepartmentSaveCommand;
import vip.dexian.admin.command.DepartmentUpdateCommand;
import vip.dexian.admin.enums.OperateTypeEnum;
import vip.dexian.admin.log.AdminLog;
import vip.dexian.admin.service.DepartmentService;
import vip.dexian.common.Message;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 部门controller
 *
 * @author 挺好的 2023年05月31日 9:50
 */
@Controller ("adminDepartmentController")
@RequestMapping ("/admin/department")
public class DepartmentController extends BaseController {

    /**
     * 部门service
     */
    @Resource (name = "departmentServiceImpl")
    private DepartmentService departmentService;

    /**
     * 部门列表页面
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return list view
     */
    @GetMapping ("/list")
    @AdminLog (module = "department", description = "查看部门列表", type = OperateTypeEnum.LIST)
    public String list (ModelMap model) {
        model.addAttribute("tree", this.departmentService.findTree());
        return "/admin/admin/department/list";
    }

    /**
     * 进入保存页面
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return save view
     */
    @GetMapping ("/save")
    public String save (ModelMap model) {
        model.addAttribute("tree", this.departmentService.findTree());
        return "/admin/admin/department/save";
    }

    /**
     * 保存部门
     *
     * @param command
     *         部门保存command
     *
     * @return {@link Message}
     */
    @PostMapping ("/save")
    @ResponseBody
    @AdminLog (module = "department", description = "保存部门", type = OperateTypeEnum.SAVE)
    public Message save (DepartmentSaveCommand command) {
        this.departmentService.save(command);
        return Message.success();
    }

    /**
     * 进入编辑页面
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return update view
     */
    @GetMapping ("/update")
    public String update (ModelMap model, Long id) {
        model.addAttribute("tree", this.departmentService.findTree());
        model.addAttribute("content", this.departmentService.find(id));
        return "/admin/admin/department/update";
    }

    /**
     * 更新部门
     *
     * @param command
     *         部门更新command
     *
     * @return {@link vip.dexian.common.Message}
     */
    @PostMapping ("/update")
    @ResponseBody
    @AdminLog (module = "department", description = "更新部门", type = OperateTypeEnum.UPDATE)
    public Message update (DepartmentUpdateCommand command) {
        this.departmentService.update(command);
        return Message.success();
    }

    /**
     * 删除部门
     *
     * @param id
     *         部门id
     *
     * @return {@link Message}
     */
    @PostMapping ("/delete")
    @ResponseBody
    @AdminLog (module = "department", description = "删除部门", type = OperateTypeEnum.DELETE)
    public Message delete (Long id) {
        this.departmentService.delete(id);
        return Message.success();
    }
}
